package pri.hillchen.springbootstd.sso.service.impl;

import org.springframework.stereotype.Service;
import pri.hillchen.springbootstd.sso.common.util.IdWorker;
import pri.hillchen.springbootstd.sso.entity.MenuInfo;
import pri.hillchen.springbootstd.sso.service.MenuService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:hillchen
 * @DESCRIPTION:
 * @Date: Created in 10:36 2017/12/26 0026.
 */
@Service
public class MenuServiceImpl implements MenuService {
    static {
        IdWorker.initIdWorker(10);
    }
    private IdWorker idWorker = IdWorker.getInstance();
    @Override
    public List<MenuInfo> getPrivilegeMenus(String loginName, String systemCode) {
        List<MenuInfo> menus = new ArrayList<MenuInfo>();
        MenuInfo systemMange =  createMenu("系统管理","");
        systemMange.setChildrenMenu(menus);
        MenuInfo userMange = createMenu("用户管理","/view/system/UserManger");
        MenuInfo roleMange = createMenu("角色管理","/view/system/RoleManger");
        MenuInfo resourceMange = createMenu("资源管理","/view/system/ResourceManger");
        MenuInfo privilegeManger = createMenu("权限管理","/view/system/PrivilegeManger");
        menus.add(userMange);
        menus.add(roleMange);
        menus.add(resourceMange);
        menus.add(privilegeManger);

        List<MenuInfo> retMenus = new ArrayList<>();
        retMenus.add(systemMange);
        return retMenus;
    }

    private MenuInfo createMenu(String menuName,String routerPath){

        MenuInfo menuInfo = new MenuInfo();
        menuInfo.setId(idWorker.nextId()+"");
        menuInfo.setMenuName(menuName);
        menuInfo.setIcon("folder");
        menuInfo.setRouterPath(routerPath);
        return menuInfo;
    }

    private MenuInfo createRootMenu(int menuSort,String sysCode){
        MenuInfo rootMenu = new MenuInfo();
        rootMenu.setId(idWorker.nextId()+"");
        rootMenu.setMenuName(String.format("根菜单%s",menuSort));
        rootMenu.setSystemCode(sysCode);
        rootMenu.setIcon("folder");
        rootMenu.setChildrenMenu(createChildMenus(sysCode));
        rootMenu.setSort(menuSort);
        return rootMenu;
    }

    private List<MenuInfo> createChildMenus(String sysCode){
        List<MenuInfo> childMenus = new ArrayList<>();
        childMenus.add(createChildMenu(1,sysCode,1,3));
        childMenus.add(createChildMenu(2,sysCode,1,0));
        childMenus.add(createChildMenu(3,sysCode,1,3));
        return childMenus;
    }

    private MenuInfo createChildMenu(int menuSort,String sysCode,int level,int childCount){

        MenuInfo childMenu = new MenuInfo();
        childMenu.setId(idWorker.nextId()+"");
        childMenu.setMenuName(String.format("%s级子菜单%s",level,menuSort));
        childMenu.setSystemCode(sysCode);
        childMenu.setIcon("flame");
        childMenu.setSort(menuSort);
        if(childCount != 0 ){

            List<MenuInfo> childMenus = new ArrayList<>();
            if(childCount == 3){
                childMenus.add(createChildMenu(1,sysCode,level+1,2));
                childMenus.add(createChildMenu(2,sysCode,level+1,0));
                childMenus.add(createChildMenu(3,sysCode,level+1,2));
            }else{
                for(int i =1 ; i<= childCount; i++){
                    childMenus.add(createChildMenu(1,sysCode,level+1,0));
                }
            }
            childMenu.setChildrenMenu(childMenus);
        }

        return childMenu;
    }
}
